//========encoding:utf-8=========
/*
 * @describe:  
 * @author:cjgong
 * @copyright:chinasoft.cn
 * @date:2020年1月17日 上午10:49:54 
 */

package edu.chinasoft.service;

import edu.chinasoft.dao.UserDao;
import edu.chinasoft.exception.UserNoExistsException;
import edu.chinasoft.pojo.PageUtilForUser;
import edu.chinasoft.pojo.User;
import org.apache.log4j.Logger;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Propagation;
import org.springframework.transaction.annotation.Transactional;

import java.beans.Transient;


@Service(value = "userService")
public class UserServiceImp implements UserService {
	private Logger logger = Logger.getLogger(UserServiceImp.class);
	private UserDao userDao;
	@Transactional(readOnly = false,propagation = Propagation.REQUIRED)
	@Override
	public User addUser(User user) {
		logger.debug("UserServiceImp>>>addUser  start ……");
		User result = userDao.insertTable(user);
		logger.debug("UserServiceImp>>>addUser  end ……");
		return result;
	}

	@Transactional(readOnly = false,propagation = Propagation.REQUIRED)
	@Override
	public boolean xiaoMing_XiaoHong_Account(User xiaoMing, User xiaoHong, double accouont){
		//开启一个事务
		//1   开启事务——————检查当前有没有事务，如果有事务，使用当前事务；如果没有事务，开启1个新事务
		boolean result01 = userDao.updateTable_Account(xiaoMing, accouont);
		//int num=5/0;
		//1   开启事务——————检查当前有没有事务，如果有事务，使用当前事务；如果没有事务，开启1个新事务
		boolean result02=userDao.updateTable_Account(xiaoHong,-accouont);

		//提交事务
		return result01&&result02;
	}

	@Transactional(readOnly = true,propagation = Propagation.SUPPORTS)
	@Override
	public PageUtilForUser findUser(int currentPage, int pageUtil) {
		logger.debug("UserServiceImp>>>findGoods  start ……");
		PageUtilForUser result = userDao.selectTablePage(currentPage, pageUtil);

		logger.debug("UserServiceImp>>>findGoods  end ……");
		return result;
	}
	@Transactional(readOnly = true,propagation = Propagation.SUPPORTS)
	@Override
    public User  findUserInfo(User user) {
    	logger.debug("UserServiceImp>>>findUserInfo  start ……");
    	User result = this.userDao.selectUserById(user);
    	logger.debug("UserServiceImp>>>findUserInfo  start ……");
    	return result;
    }
	@Transactional(readOnly = false,propagation = Propagation.REQUIRED)
	@Override
    public boolean  modifyUserInfo(User user) {
    	logger.debug("UserServiceImp>>>modifyUserInfo  start ……");
    	boolean result = this.userDao.updateUserById(user);
    	logger.debug("UserServiceImp>>>modifyUserInfo  start ……");
    	return result;
    }
	@Transactional(readOnly = false,propagation = Propagation.REQUIRED)
	@Override
    public boolean  modifyUserFlag(User user) {
    	logger.debug("UserServiceImp>>>modifyUserFlag  start ……");
    	boolean result = this.userDao.updateUserFlagById(user);
    	logger.debug("UserServiceImp>>>modifyUserFlag  start ……");
    	return result;
    }
	@Transactional(readOnly = false,propagation = Propagation.REQUIRED)
	@Override
    public boolean  deleteUserInfo(User user) {
    	logger.debug("UserServiceImp>>>deleteUserInfo  start ……");
    	boolean result = this.userDao.deleteUserById(user);
    	logger.debug("UserServiceImp>>>deleteUserInfo  start ……");
    	return result;
    }
	@Transactional(readOnly = true,propagation = Propagation.SUPPORTS)
	@Override
	public User  loginUser(User user) throws UserNoExistsException{
		logger.debug("UserServiceImp>>>loginUser  start ……");
		User result = this.userDao.selectUserByNameAndPassword(user);
		logger.debug("UserServiceImp>>>loginUser  end ……");
		return result;
	}
	@Transactional(readOnly = true,propagation = Propagation.SUPPORTS)
	@Override
	public boolean  selectUserByName(User user) {
		logger.debug("UserServiceImp>>>selectUserByName  start ……");
		boolean result = this.userDao.selectUserByName(user);
		logger.debug("UserServiceImp>>>selectUserByName  end ……");
		return result;
	}

	public UserDao getUserDao() {
		return userDao;
	}
	@Autowired
	@Qualifier(value = "userDao")
	public void setUserDao(UserDao userDao) {
		this.userDao = userDao;
	}
}
